home *** CD-ROM | disk | FTP | other *** search
- Path: news2.ios.com!usenet
- From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
- Newsgroups: comp.lang.c++
- Subject: Re: Template casting problem
- Date: Sat, 27 Jan 1996 08:14:11 GMT
- Organization: Internet Online Services
- Message-ID: <4ecmfm$as9@news2.ios.com>
- References: <41@site73.site73.ping.at>
- NNTP-Posting-Host: ppp-33.ts-7.hck.idt.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- mgeramb@site73.site73.ping.at (Michael Geramb) wrote:
-
- >Here is a C++ problem that I never managed to solve.
-
- >Imagine you create a template of a class :
-
- >template <class T> class A
- >{
- > ...
- >};
-
- >This gives a family of classes (instances), say:
-
- >A<short>, A<int>, A<float> etc, which may coexist in one function.
- >Then, a problem arises how to cast one instance to another.
-
- If you like to complicate things, so maybe this can be part of your
- solution:
-
-
-
- template <class T> class A
- {
- T x;
- public:
- A(T i) : x(i) {}
- operator float() {return (float) x;}
- operator int() {return (int) x;}
- operator short() {return (short) x;}
- operator double() {return (double) x;}
- A(A<int> & i): x((T)i) {}
- };
-
-
- //example
-
- void main()
- {
- A<int> a(6);
- A<float> x = a;
- A<short> i = x;
- A<double> z = i;
- };
-
-
- >As a more concrete example, one may imagine a template CVector which
- >generates short, integer, float and double instances.
-
- What do you mean by that CVector generates short,integer,float etc?
- Here I got lost.
-
-
-
- *******************************************
- * Vlastimil Adamovsky *
- * Smalltalk, C++ and Envelop development *
- *******************************************
-
-